const path = require('path');
const fs = require('fs');

const lineByLine = require('n-readlines');
const { filesize } = require('filesize');
const libqqwry = require('lib-qqwry');
const qqwry = libqqwry(true, __dirname + "/qqwry.dat");
const _ = require('lodash');
const dayjs = require('dayjs');
const term = require('terminal-kit').terminal;
const open = require('open');

let danger_address = [
    'CZ88.NET',
    '数据中心',
    '法国',
    '华数',
    'Cogent',
    '荷兰',
    '俄罗斯',
    '德国',
    '罗马尼亚',
    '波兰',
    '保加利亚',
    '英国',
    '加拿大',
    '捷克',
    '巴林',
    '澳大利亚',
    '台湾',
];


function get_ip(str) {
    let reg = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g
    let array = str.match(reg);

    if (!array) return '';

    return array[0];
}

function get_address(ip) {
    try {
        let result = qqwry.searchIP(ip);
        if (typeof (result.Country) != 'undefined' && typeof (result.Area) != 'undefined') {
            return result.Country + result.Area;
        } else {
            return '';
        }
    } catch (e) {
        return '';
    }
}


function get_is_danger(address) {
    let result = false;
    for (let d of danger_address) {
        if (address.indexOf(d) > -1) {
            result = true;
            break;
        }
    }
    return result;
}


async function to_html(html_file, rows) {
    let content = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ip分析结果</title>
<style>
body{
	font-size:18px;
	line-height:30px;
}
table{
	border-collapse:collapse;
	border:1px solid skyblue;
}
</style>
</head>
<body>
<h2 style="text-align:center;">ip分析结果</h2>

<table width="100%" border="1" align="center">
  <tbody>
    <tr>
      <td>No.</td>
      <td>ip</td>
      <td>地址</td>
      <td>次数</td>
      <td>文本</td>
    </tr>
    ${rows.map((v, k) => {
        let is_danger = get_is_danger(v.address);
        return `
        <tr bgcolor="${is_danger ? '#FF9966' : ''}">
        <td>${k + 1}</td>
        <td><a href="https://www.ip138.com/iplookup.php?ip=${v.ip}&action=2" target="_blank">${v.ip}</td>
        <td>${v.address}</td>
        <td>${v.count}</td>
        <td><textarea rows="10" cols="120">${v.content}</textarea></td>
      </tr>
        `;
    }).join('')}
    
  </tbody>
</table>

</body>
</html>    
`;
    await fs.promises.writeFile(html_file, content, { encoding: 'utf8' });

    return true;
}

async function start() {
    let arguments = process.argv;

    if (typeof (arguments[2]) == 'undefined') {
        console.log('请输入文件名');
        console.log('node index.js 1.log');
        process.exit();
        return;
    }

    try {
        let START_TIME = dayjs().unix();

        let file = arguments[2];
        let stat = await fs.promises.stat(file);
        console.log('文件大小：', filesize(stat.size, { standard: "jedec" }));
        console.log('正在分析中...');

        let IPS = {};

        const liner = new lineByLine(file);

        let line;

        while (line = liner.next()) {
            let content = line.toString('ascii');
            let ip = get_ip(content.substr(0, 50));
            if (!ip) continue;

            if (typeof (IPS[ip]) != 'undefined') {
                IPS[ip]['count']++;
                if (IPS[ip]['count'] <= 20) {
                    IPS[ip]['content'] += `\n\n${content}`;
                }

            } else {
                let address = get_address(ip);
                IPS[ip] = {
                    ip: ip,
                    address: address,
                    count: 1,
                    content: content,
                }
            }


        }

        //过滤count>100
        let FINAL_IPS = _.filter(Object.values(IPS), function (o) {
            return o.count > 100;
        });

        FINAL_IPS = _.sortBy(FINAL_IPS, function (o) {
            return -o.count;
        });

        let END_TIME = dayjs().unix();


        console.log('分析完毕，耗时：', END_TIME - START_TIME, '秒');

        //保存结果
        let html_file = `${__dirname}/result/result.html`;
        await to_html(html_file, FINAL_IPS);

        console.log('结果文件已保存至：', html_file);


    } catch (err) {
        console.error(err);
    }


    process.exit();
}

start();